const App = getApp();

// 工具类
import util from '../../utils/util.js';

// 倒计时插件
import CountDown from '../../utils/countdown.js';

// 枚举类：秒杀会场活动状态
import StateEnum from '../../utils/enum/sharp/ActiveStatus.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {

    // 当前tab索引
    curTabIndex: 0,

    noMore: false, // 没有更多数据
    isLoading: true, // 是否正在加载中
    page: 1, // 当前页码

    StateEnum, // 枚举类：秒杀会场活动状态

    // 倒计时
    countDownList: [],

    // 秒杀活动场次
    tab: [],

    // 秒杀商品列表
    goodsList: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    let active_id = options.active_id
    _this.setData({
      active_id:active_id
    })
    _this.getDiscountList();
  },
 

  /**
   * 获取列表数据
   */
  getDiscountList(isPage, page) {
    let _this = this;
    App._requestGet(_this, App.globalData.config.apiDiscountIndexUrl, {
      page: page || 1,
      dataType: _this.data.dataType,
      active_id:_this.data.active_id
    }, result => {
      let resList = result.data.goodsList;
      let Discountactive = result.data.goodsList.active;
      let started = '';
      if(resList){
        if (Discountactive.active_status == 10){
          started = Discountactive.end_date - Date.parse(new Date())  / 1000     //已经开始时间差
        } else{
          started =Discountactive.start_date  - Date.parse(new Date() ) / 1000    //即将开始时间差
        }
      }
      if (isPage == true) {
        _this.setData({
          'list.data':resList.data,
          isLoading: false,
          Discountactive,
          started
        });
      } else {
        _this.setData({
          list: resList,
          isLoading: false,
          Discountactive,
          started
        });
      }
      var interval = setInterval(function () {
        var second = started ;

        var day = Math.floor(second / 3600 / 24);
        var hr = Math.floor((second - day * 3600 * 24) / 3600);
        var hrStr = hr.toString();
        if (hrStr.length == 1) hrStr = '0' + hrStr;
        // 分钟位
        var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
        var minStr = min.toString();
        if (minStr.length == 1) minStr = '0' + minStr;

        // 秒位
        var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
        var secStr = sec.toString();
        if (secStr.length == 1) secStr = '0' + secStr;

        _this.setData({
          countDownDay: day,
          countDownHour: hrStr,
          countDownMinute: minStr,
          countDownSecond: secStr,
        });
        started--;
      }.bind(this), 1000);
      if (resList.data.length > 0 && resList.last_page * resList.per_page >= resList.total && resList.current_page == resList.last_page) {
        this.setData({
          no_more: true
        });
      }
    });
  },


  /**
   * 切换tabbar
   */
  onToggleTab(e) {
    let _this = this;
    // 保存formid
    App.saveFormId(e.detail.formId);
    // 设置当前tabbar索引，并重置数据
    _this.setData({
      curTabIndex: e.detail.target.dataset.index,
      goodsList: [],
      page: 1,
      isLoading: true,
      noMore: false,
    });
    // 获取列表数据
    _this.getDiscountList();
    // 初始化倒计时组件
    _this._initCountDownData();
  },
  //跳转到详情页
	jumpView: function(e) {
		let url = e.currentTarget.dataset.url
		if (!url) {
      let field = e.currentTarget.dataset.field;
      console.log(e)
			if (!field) {
				this.showError('jumpView事件的同一元素上缺少属性 data-field')
				return false;
			}
			let aid = field.aid,
        active_id = field.active_id;
			if (!url && aid > 0) {
        url = `/pages/archives/product/discount_detail?aid=${aid}&active_id=${active_id}`
      }
    }
		wx.navigateTo({
			url: url
		})
	},
  /**
   * 跳转到秒杀商品详情
   */
  // onTargetActive(e) {
  //   let _this = this;
  //   console.log(e)
  //   // 保存formid
  //   App.saveFormId(e.detail.formId);
  //   let query = ({
  //     active_id:e.detail.target.actived,
  //     aid: e.detail.target.dataset.id,
  //   });
  //   wx.navigateTo({
  //     url: `/pages/archives/product/discount_detail?${query}`,
  //   })
  // },

  /**
   * 下拉到底部加载下一页
   */
  onReachBottom() {
    let _this = this,
      listData = _this.data.goodsList;
    // 已经是最后一页
    if (_this.data.page >= listData.last_page) {
      return false;
    }
    // 加载下一页列表
    _this.setData({
      page: ++_this.data.page
    });
    _this.getGoodsList(true);
  },

  /**
   * 获取列表数据
   */
  // getGoodsList(isNextPage) {
  //   let _this = this,
  //   curTabbar = _this.data.tab[_this.data.curTabIndex];
  //   App._requestGet(_this, App.globalData.config.SharpGoodsIndexUrl, {
  //     page: _this.data.page || 1,
  //     // active_time_id: curTabbar.active_time_id
  //   }, (result) => {
  //     let resList = result.data.goodsList,
  //       dataList = _this.data.goodsList;
  //     if (isNextPage == true) {
  //       _this.setData({
  //         'goodsList.data': dataList.data.concat(resList.data),
  //         isLoading: false,
  //       });
  //     } else {
  //       _this.setData({
  //         goodsList: resList,
  //         isLoading: false,
  //       });
  //     }
  //     if( resList.data.total > 0 && resList.data.current_page == resList.data.last_page){
  //       _this.setData({
  //         noMore:true
  //       });
  //     }
  //   });
  // },
   /**
   * 活动规则弹窗
   */
  showservicePopup(e) {
    let _this = this;
    _this.setData({
      showservicePopup: !_this.data.showservicePopup,
    });
  },
  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    const _this = this;
    // 构建页面参数
    const params = App.getShareUrlParams();
    return {
      title: '整点秒杀',
      path: `/pages/sharp/index/index?${params}`
    };
  },
	onShareTimeline() {
		let _this = this;
		return {
			title: '整点秒杀',
		}
	},
  /**
   * 分享到朋友圈
   * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
   * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
   */
  onShareTimeline() {
    const _this = this;
    // 构建页面参数
    const params = App.getShareUrlParams();
    return {
      title: '整点秒杀',
      path: `/pages/sharp/index/index?${params}`
    };
  },

})